#coding:utf-8
import urllib2,urllib,re

class JD_DOG(object):
    def __init__(self):
        self.pageindex = 1
        self.cur_url = 'https://search.jd.com/Search?keyword=%E7%8B%97%E7%B2%AE&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&offset=1&wq=%E7%8B%97%E7%B2%AE&stock=1&page={page}'

    def get_goods_page(self,pageindex):
        url = self.cur_url.format(page=pageindex)
        request = urllib2.Request(url)
        response = urllib2.urlopen(request)
        aa= response.read().decode('utf-8')
        return aa

    def get_good_detail(self,pageindex):
        page = self.get_goods_page(pageindex)
        pattern = re.compile(r'<li data-sku=".*?</li>',re.S)
        contents = re.findall(pattern,page)
        return contents

    def get_good_items(self,pageindex):
        data = self.get_good_detail(pageindex)
        patt = re.compile(r'<li data-sku=".*?<div class="gl-i-wrap.*?<div class="p-img.*?<a target="_bla.*?title="(.*?)" href="(.*?)" onclick=.*?<img width=.*?class=".*?data-img=.*?//(.*?)" />.*?<div data-catid.*?</div>.*?<div class="p-price.*?<strong class.*?<em>.*?<i>(.*?)</i>.*?</li>',re.S)
        for line in data:
            content = re.findall(patt,line)
            print content[0][0]+'~~~~'+content[0][1]+'~~~~'+content[0][2]+'~~~~'+content[0][3]


    def start(self):
        while self.pageindex <= 11:
            self.get_good_items(self.pageindex)
            self.pageindex += 2


spider = JD_DOG()
spider.start()